//
//  HDCFRecommendViewController.m
//  HaoDeCaiFu
//
//  Created by 张玉鹏 on 2017/11/16.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFRecommendViewController.h"
#import "RecommendPerson.h"
#import "ErWeiMaViewController.h"
@interface HDCFRecommendViewController ()

@property (nonatomic, weak) UILabel *codeLabel;

@end

@implementation HDCFRecommendViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden=YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=ZKcolor(245, 245, 245);
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, -20, screenWidth, screenHeight+20)];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=W_RATIO(100);
    _tableView.sectionHeaderHeight=W_RATIO(100);
    [self.view addSubview:_tableView];
    [_tableView setTableHeaderView:[self TableViewHeaderView]];
    _tableView.backgroundColor=ZKcolor(245, 245, 245);
    [_tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    [self Nav];
    [self getMyRecommendPersonData];
    [self getUserId];
    
}

- (void)getUserId{
    [HttpRequest sendSelfGetRequestWithURL:@"/api/User/GetUserId" WithParamets:nil success:^(id result) {
        
        NSLog(@"%@",result);
        NSDictionary*resultDic=(NSDictionary*)result;
        self.ResultArray = [RecommendPerson mj_objectArrayWithKeyValuesArray:[resultDic objectForKey:@"result"]];
        NSNumber *code = result[@"result"];
        self.codeLabel.text = code.stringValue;
        
    } failure:^(NSError *errer) {
        
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

-(void)getMyRecommendPersonData
{
    [SVProgressHUD show];
    [HttpRequest sendSelfGetRequestWithURL:@"/api/User/GetSubset" WithParamets:nil success:^(id result) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",result);
        NSDictionary*resultDic=(NSDictionary*)result;
        self.ResultArray = [RecommendPerson mj_objectArrayWithKeyValuesArray:[resultDic objectForKey:@"result"]];
        [_tableView reloadData];
        
    } failure:^(NSError *errer) {
        
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
    
}


-(void)Nav
{
    UILabel*titleLable=[[UILabel alloc]initWithFrame:CGRectMake(W_RATIO(220), W_RATIO(50), screenWidth-W_RATIO(440), W_RATIO(60))];
    titleLable.text=@"我的推荐";
    titleLable.textColor=[UIColor whiteColor];
    titleLable.font=[UIFont systemFontOfSize:17];
    titleLable.textAlignment=1;
    [self.view addSubview:titleLable];
    
    
    UIButton*backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(10, W_RATIO(50), W_RATIO(60), W_RATIO(60));
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    
    UIButton*rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame=CGRectMake(screenWidth-W_RATIO(150)-10, W_RATIO(50), W_RATIO(150), W_RATIO(60));
    [rightBtn setTitle:@"分享邀请码" forState:UIControlStateNormal];
    rightBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightBtn];
    
    
}

-(void)rightBtnClick:(UIButton*)btn
{
    ErWeiMaViewController*vc=[[ErWeiMaViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)backBtnClick:(UIButton*)btn
{
    self.navigationController.navigationBarHidden=NO;
    [self.navigationController popViewControllerAnimated:YES];
}

-(UIImageView*)TableViewHeaderView
{
    UIImageView*headerImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, W_RATIO(600))];
    headerImageView.image=[UIImage imageNamed:@"recommend"];
    UILabel *codeLabel = [[UILabel alloc] init];
    self.codeLabel = codeLabel;
    codeLabel.backgroundColor = [UIColor clearColor];
    codeLabel.frame = CGRectMake(0, KNavHeight, screenWidth, headerImageView.height - KNavHeight);
    codeLabel.text = @"";
    codeLabel.textColor = [UIColor whiteColor];
    codeLabel.textAlignment = NSTextAlignmentCenter;
    codeLabel.font = [UIFont boldSystemFontOfSize:21];
    [headerImageView addSubview:codeLabel];
    return headerImageView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ResultArray.count;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel*titleLable=[[UILabel alloc]initWithFrame:CGRectMake(-5, -1, screenWidth+10, W_RATIO(80))];
    
    titleLable.text=[NSString stringWithFormat:@"推荐人数：%lu人",self.ResultArray.count];
    titleLable.font=[UIFont systemFontOfSize:15];
    titleLable.textAlignment=1;
    titleLable.layer.borderColor=[UIColor lightGrayColor].CGColor;
    titleLable.layer.borderWidth=0.6;
    titleLable.backgroundColor=[UIColor whiteColor];
    return titleLable;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    else
    {
        for (UIView*view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
    }
    
    RecommendPerson*person=self.ResultArray[indexPath.row];
    
    UILabel*nameLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth/2-10, W_RATIO(100))];
    nameLable.font=[UIFont systemFontOfSize:15];
    nameLable.text=person.Name;
    [cell.contentView addSubview:nameLable];
    
    
    UILabel*timeLable=[[UILabel alloc]initWithFrame:CGRectMake(nameLable.right, 0, nameLable.width, W_RATIO(100))];
    timeLable.font=[UIFont systemFontOfSize:13];
    NSArray*array=[person.CreateTime componentsSeparatedByString:@"T"];
    timeLable.text=array[0];
    timeLable.textColor=[UIColor lightGrayColor];
    timeLable.textAlignment=2;
    [cell.contentView addSubview:timeLable];
    
    
    
    return cell;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
